//
//  MyCollectionViewController.swift
//  SCUTWiki
//
//  Created by zhw on 2020/12/15.
//  Copyright © 2020 wwszhw. All rights reserved.
//

import UIKit

class HomeCollectionViewController: UICollectionViewController, UISearchControllerDelegate, UISearchResultsUpdating{

    var searchController: UISearchController?
    var sqlManager = SQLiteManager.shareInstance
    var subjectArray = [SubjectModel]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //self.collectionView.contentInset.top = searchController.searchBar.frame.size.height

        searchController = UISearchController(searchResultsController: SearchResultsTableViewController())
        searchController?.searchBar.searchBarStyle = UISearchBar.Style.minimal//设置bar的样式
        searchController?.delegate=self
        searchController?.searchResultsUpdater = self
        self.navigationItem.searchController = searchController
        self.navigationItem.hidesSearchBarWhenScrolling = false
        definesPresentationContext = true
        
        loadSubject()
    }

    
    // MARK: UICollectionViewDataSource
    func loadSubject() -> Void {
        if sqlManager.openDB(){
            if let subjects = sqlManager.execQuerySQL(sql: "select * from subject"){
                for row in subjects{
                    subjectArray.append(SubjectModel(type_id: row["type_id"] as! Int, subject_name: row["subject_name"] as! String, intro: row["intro"] as! String))
                }
            }
        }
        sqlManager.closeDB()
    }
    

    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }


    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return subjectArray.count
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "reuseCollectionCell", for: indexPath) as! MyCollectionViewCell
        cell.collectionCellImage.image = UIImage(named: subjectArray[indexPath.row].subject_name)
        cell.collectionCellLabel.text = subjectArray[indexPath.row].subject_name
    
        return cell
    }

    
    // MARK: - Navigation
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using [segue destinationViewController].
        if let dest_view_controller = segue.destination as? SubjectTableViewController{
            if let select_index = self.collectionView.indexPathsForSelectedItems?.first{
                dest_view_controller.subject_name = subjectArray[select_index.row].subject_name
                dest_view_controller.type_id = subjectArray[select_index.row].type_id
                dest_view_controller.intro = subjectArray[select_index.row].intro
            }
        }
        // Pass the selected object to the new view controller.
    }
    
    func updateSearchResults(for searchController: UISearchController) {
        let resultController = searchController.searchResultsController as! SearchResultsTableViewController
        //去除头尾的空格
        let whitespaceCharacterSet = CharacterSet.whitespaces
        let strippedString = searchController.searchBar.text!.trimmingCharacters(in: whitespaceCharacterSet)
        let searchItems = strippedString
        
        resultController.searchArticles(title: searchItems)
    }
    
    // MARK: UICollectionViewDelegate

    /*
    // Uncomment this method to specify if the specified item should be highlighted during tracking
    override func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool {
        return true
    }
    */

    /*
    // Uncomment this method to specify if the specified item should be selected
    override func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
        return true
    }
    */

    /*
    // Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
    override func collectionView(_ collectionView: UICollectionView, shouldShowMenuForItemAt indexPath: IndexPath) -> Bool {
        return false
    }

    override func collectionView(_ collectionView: UICollectionView, canPerformAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
        return false
    }

    override func collectionView(_ collectionView: UICollectionView, performAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) {
    
    }
    */

}
